﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using DataAccessLayer;
using System.Data;
using System.Configuration;
using DTO;

namespace ABCComputer.DAL
{
    public class SanPhamDAL
    {
        private DBManager dbmanager;

        public SanPhamDAL()
        {
            string connectionInfo = ConfigurationManager.AppSettings["ConnectionInfo"];
            dbmanager = new DBManager(DataProvider.SqlServer, connectionInfo);
        }

        private SanPhamDTO createSanPhamFromReader(IDataReader reader)
        {
            SanPhamDTO sp = new SanPhamDTO();

            sp.MaSanPham = Convert.ToString(reader["MaSanPham"]);
            sp.NhomSanPham = Convert.ToString(reader["MaNhomSanPham"]);
            sp.NhaSanXuat = Convert.ToString(reader["MaNhaSanXuat"]);
            sp.TenSanPham = Convert.ToString(reader["TenSanPham"]);
            sp.TinhTrang = (int) reader["TinhTrang"];
            sp.SoLuongTonKho = (int)reader["SoLuongTonKho"];
            sp.GiaBan = Convert.ToDecimal(reader["GiaBan"]);
            sp.GiaNhap = Convert.ToDecimal(reader["GiaNhap"]);
            sp.KhuyenMai = (int)reader["KhuyenMai"];
            sp.ThongSoKyThuat = Convert.ToString(reader["ThongSoKyThuat"]);
            sp.SoLuotMua = (int)reader["SoLuotMua"];
            sp.NgayDang = Convert.ToDateTime(reader["NgayDang"]);
            sp.BaoHanh = (int) (reader["BaoHanh"]);
            
            return sp;
        }

        // lấy toàn bộ thông tin sản phẩm
        public List<SanPhamDTO> getListSanPham()
        {
            List<SanPhamDTO> lst = new List<SanPhamDTO>();
            IDataReader reader = null;
            try
            {
                dbmanager.Open();

                reader = dbmanager.ExecuteReader(CommandType.StoredProcedure, "sp_getListSanPham");
                while (reader.Read())
                    lst.Add(createSanPhamFromReader(reader));
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
            finally
            {
                if (reader != null)
                    reader.Close();
                dbmanager.Close();
            }
            return lst;
        }

        // lay thong tin 1 san pham
        public SanPhamDTO getSanPham(string masp)
        {
            SanPhamDTO sp = new SanPhamDTO();
            IDataReader reader = null;
            try
            {
                dbmanager.Open();

                dbmanager.CreateParameters(1);
                dbmanager.AddParameters(0, "@masp", masp);
                reader = dbmanager.ExecuteReader(CommandType.StoredProcedure, "sp_getSanPham");
                while (reader.Read())
                {
                    sp = createSanPhamFromReader(reader);
                }
                    
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
            finally
            {
                if (reader != null)
                    reader.Close();
                dbmanager.Close();
            }
            return sp;
        }

        // lay thong tin 1 san pham co hinh anh
        public List<SanPhamFullDTO> getSanPhamFull(string masp)
        {
            List<SanPhamFullDTO> lstSPF = new List<SanPhamFullDTO>();
            IDataReader reader = null;
            try
            {
                dbmanager.Open();

                dbmanager.CreateParameters(1);
                dbmanager.AddParameters(0, "@masp", masp);
                reader = dbmanager.ExecuteReader(CommandType.StoredProcedure, "sp_getSanPham");
                while (reader.Read())
                {
                    SanPhamDTO sp = new SanPhamDTO();
                    SanPhamFullDTO spF = new SanPhamFullDTO();
                    sp = createSanPhamFromReader(reader);

                    spF.MaSanPham = sp.MaSanPham;
                    spF.TenSanPham = sp.TenSanPham;
                    spF.TinhTrang = sp.TinhTrang;
                    spF.BaoHanh = sp.BaoHanh;
                    spF.GiaBan = sp.GiaBan;
                    spF.ThongSoKyThuat = sp.ThongSoKyThuat;
                    spF.SoLuongTonKho = sp.SoLuongTonKho;
                   

                    HinhAnhDAL haDAL = new HinhAnhDAL();
                    List<HinhAnhDTO> lstHA = new List<HinhAnhDTO>();
                    lstHA = haDAL.getListHinhAnhMotSanPham(spF.MaSanPham);

                    spF.LstHinhAnh = lstHA;
                    lstSPF.Add(spF);
                }

            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
            finally
            {
                if (reader != null)
                    reader.Close();
                dbmanager.Close();
            }
            return lstSPF;
        }

        // lay thong tin san pham cho gio hang
        public GioHangDTO getSanPhamVaoGioHang(string masp)
        {
            GioHangDTO gh = new GioHangDTO();
            IDataReader reader = null;
            try
            {
                dbmanager.Open();

                dbmanager.CreateParameters(1);
                dbmanager.AddParameters(0, "@masp", masp);
                reader = dbmanager.ExecuteReader(CommandType.StoredProcedure, "sp_getSanPhamVaoGioHang");
                while (reader.Read())
                {
                    gh.MaSanPham = reader["masanpham"].ToString();
                    gh.TenSanPham = Convert.ToString(reader["TenSanPham"]);
                    gh.SoLuong = (int)reader["SoLuongTonKho"];
                    gh.GiaBan = Convert.ToDecimal(reader["GiaBan"]);
                }

            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
            finally
            {
                if (reader != null)
                    reader.Close();
            }
            return gh;
        }

        // lấy danh sách sản phẩm mới 
        public List<SanPhamDTO> getListSanPhamMoi()
        {
            List<SanPhamDTO> lst = new List<SanPhamDTO>();
            IDataReader reader = null;
            try
            {
                dbmanager.Open();

                reader = dbmanager.ExecuteReader(CommandType.StoredProcedure, "sp_getListSanPhamMoi");
                while (reader.Read())
                    lst.Add(createSanPhamFromReader(reader));
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
            finally
            {
                if (reader != null)
                    reader.Close();
            }
            return lst;
        }

        
        // thu nghiem lay san pham co list hinh anh
        public List<SanPhamFullDTO> getListSanPhamHot()
        {
            List<SanPhamFullDTO> lst = new List<SanPhamFullDTO>();
            IDataReader reader = null;
            CommonDAL cmDAL = new CommonDAL();
            int soluotmua = cmDAL.laySoLuotMuaGoiLaHot();

            try
            {
                dbmanager.Open();

                dbmanager.CreateParameters(1);
                dbmanager.AddParameters(0, "@soluotmua", soluotmua);
                reader = dbmanager.ExecuteReader(CommandType.StoredProcedure, "sp_getListSanPhamHot");
                
                while (reader.Read())
                {
                    SanPhamFullDTO sp = new SanPhamFullDTO();
                    sp.MaSanPham = Convert.ToString(reader["MaSanPham"]);
                    sp.TenSanPham = Convert.ToString(reader["TenSanPham"]);
                    sp.GiaBan = Convert.ToDecimal(reader["GiaBan"]);

                    
                    List<HinhAnhDTO> lstHA = new List<HinhAnhDTO>();
                    HinhAnhDAL haDAL = new HinhAnhDAL();
                    lstHA = haDAL.getListHinhAnhMotSanPham(sp.MaSanPham);
                    sp.LstHinhAnh = lstHA;

                    lst.Add(sp);
                }
                
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
            finally
            {
                if (reader != null)
                    reader.Close();
                dbmanager.Close();
            }
            return lst;
        }

        
    }
}